import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import render from '@dfeidao/atom-web/render/render';
// import render_tpl from '@dfeidao/atom-web/render/data-render';
// import { fromString, place, Position, remove } from '@dfeidao/core/dom';
// import { IHandle as Handle } from '@dfeidao/core/on';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';

import a015 from './a015';
import P02 from './P02';
interface Aa {
	param: {
		field: object[]
	};
	field: unknown;
}
interface Bb {
	param: {
		field: HTMLElement
	};
}
export default async function a017(fd: IFeidaoAiBrowserComponent, args: { currentTarget: { dataset: { feidaoId: string } } }) {
	const name2title = get(fd, 'name2title') as Aa;
	//  确定按钮(text)
	const field = args.currentTarget.dataset.feidaoId;

	const q = {} as Aa;
	const input_node = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id=input_' + field + ']');
	const v = input_node.value;
	if (v) {
		q[field] = v;
	} else {
		q[field] = '';
	}
	input_node.value = "";

	const condition_node = get(fd, 'condition_node') as Bb;
	const condition_evt = get(fd, 'condition_evt') as Aa;
	// console.log('--------a017---------');
	// console.log(condition_node)
	// 渲染条件
	if (condition_node.param[field]) {
		delete condition_node.param[field];
		(condition_evt.param[field][0]).destroy();
		delete condition_evt.param[field];
		const r_node = fd.data.node.querySelector("#condition_" + field);
		// remove(r_node);
		r_node.parentNode.removeChild(r_node);	// 找到删除的子元素，然后使用其 parentNode 属性来找到父元素：
	}
	const tpl_d = {
		field, field_name: name2title[field], field_value: q[field] || '空'
	};
	// const tpl_str = render_tpl(tpl_d, P02);

	// const node = fd.data.node.querySelector('[data-feidao-presentation="P02"]');
	// place(fromString(tpl_str), Position.LastIn, node);
	render(fd, tpl_d, P02, 'P02', 'lastin');
	condition_node.param[field] = fd.data.node.querySelector('[data-feidao-id=' + field + ']') as unknown;
	// condition_evt.param[field] = fd.parse_event(condition_node.param[field]);
	const criteria = get(fd, 'criteria') as Aa;
	// console.log(criteria)
	// 刷新列表
	criteria[field] = (() => {
		if (q[field] === '') {
			return '';
		} else {
			return { $like: `%${q[field]}%` };
		}
	})();
	// console.log(criteria)
	set(fd, 'condition_node', condition_node);
	set(fd, 'condition_evt', condition_evt);
	set(fd, 'criteria', criteria);
	set(fd, 'page_no', 1);
	await a015(fd);
}
